const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://admin:jsz04005301@120.48.63.39:27017'; // MongoDB 连接 URL
const dbName = 'fast'; // 数据库名称

// 将示例数据插入到 MongoDB
async function insertSampleData() {
    const client = new MongoClient(url);

    try {
        await client.connect();

        const db = client.db(dbName);

        // 作家列表（authors）
        const authors = [
            { _id: 1, name: "曹雪芹" },
            { _id: 2, name: "鲁迅" },
            { _id: 3, name: "杨绛" },
            { _id: 4, name: "钱钟书" },
            { _id: 5, name: "莫言" },
            { _id: 6, name: "余华" },
            { _id: 7, name: "刘慈欣" },
            { _id: 8, name: "张爱玲" },
            { _id: 9, name: "毛姆" },
            { _id: 10, name: "陀思妥耶夫斯基" }
        ];

        // 书籍列表（books）
        const books = [
            { _id: 1, name: "红楼梦" },
            { _id: 2, name: "鲁迅全集" },
            { _id: 3, name: "我们仨" },
            { _id: 4, name: "围城" },
            { _id: 5, name: "红高粱家族" },
            { _id: 6, name: "活着" },
            { _id: 7, name: "三体" },
            { _id: 8, name: "倾城之恋" },
            { _id: 9, name: "月亮和六便士" },
            { _id: 10, name: "罪与罚" }
        ];

        // 作家书籍映射列表（author_book_mapping）
        const authorBookMapping = [
            { author: 1, book: 1 },
            { author: 2, book: 2 },
            { author: 3, book: 3 },
            { author: 4, book: 4 },
            { author: 5, book: 5 },
            { author: 6, book: 6 },
            { author: 7, book: 7 },
            { author: 8, book: 8 },
            { author: 9, book: 9 },
            { author: 10, book: 10 },
            { author: 1, book: 4 },
            { author: 2, book: 7 },
            { author: 3, book: 5 },
            { author: 4, book: 3 },
            { author: 5, book: 2 },
            { author: 6, book: 1 },
            { author: 7, book: 10 },
            { author: 8, book: 9 },
            { author: 9, book: 8 },
            { author: 10, book: 6 }
        ];

        // 插入作家列表到 authors 集合
        await db.collection('authors').insertMany(authors);

        // 插入书籍列表到 books 集合
        await db.collection('books').insertMany(books);

        // 插入作家书籍映射列表到 author_book_mapping 集合
        await db.collection('author_book_mapping').insertMany(authorBookMapping);

        console.log('数据插入成功！');

        await showMap(db);

    } catch (err) {
        console.error('数据插入失败:', err);
    } finally {
        client.close();
    }
}

insertSampleData();

async function showMap(db) {
    const author = await db.collection('authors').findOne({ name: "钱钟书" });
    const result = await db.collection('author_book_mapping').aggregate([
        {
            $match: {
                author: author._id// 过滤作者ID为1的数据
            }
        },
        {
            $lookup: {
                from: 'authors', // 关联的集合名称
                localField: 'author',
                foreignField: '_id',
                as: 'author'
            }
        },
        {
            $lookup: {
                from: 'books', // 关联的集合名称
                localField: 'book',
                foreignField: '_id',
                as: 'book'
            }
        },
        {
            $unwind: '$author'
        },
        {
            $unwind: '$book'
        },
        {
            $project: {
                _id: 0,
                author: 1,
                book: 1
            }
        }
    ]).toArray();

    console.log(result);
}